﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaiduFanyi
{
    public partial class Form2 : Form
    {
        private TranslateResult m_TranslateResult;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form3 f3Form3=new Form3();
            f3Form3.Show();
            for (int i = 0; i < fpdCode_Sheet1.RowCount; i++)
            {
                fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["控件类型名称"].Index].Value = "复选框";
            }
        }

        /// <summary>
        /// 得到译文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetTranslate_Click(object sender, EventArgs e)
        {
            if (m_TranslateResult == null)
            {
                TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
                m_TranslateResult = translateResult;
            }

            StringBuilder sbValue = new StringBuilder();
            int i = 0;
            foreach (Trans_Result transResult in m_TranslateResult.trans_result)
            {
                string prepertyName = GetPrepertyName(transResult.dst);
                string 控件名前缀 = "";
                switch(fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["控件类型名称"].Index].Text)
                {
                    case "下拉框":
                        控件名前缀 = "cmb";
                        break;
                    case "文本框":
                        控件名前缀 = "txt";
                        break;
                    case "复选框":
                        控件名前缀 = "chk";
                        break;
                    case "数值控件":
                        控件名前缀 = "num";
                        break;
                }
                fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["译文"].Index].Text = 控件名前缀+prepertyName;
                i++;
            }

        }

        /// <summary>
        /// 生成代码文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuild_Click(object sender, EventArgs e)
        {
            //一些没用的东西
            StringBuilder sbValue = new StringBuilder();
            Dictionary<string, PropertyInfo> DictValues = new Dictionary<string, PropertyInfo>();

            for (int i = 0; i < fpdCode_Sheet1.RowCount; i++)
            {
                string line = fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["中文"].Index].Text;
                if (!DictValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    PropertyInfo property = new PropertyInfo();
                    property.ControlName = fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["控件类型名称"].Index].Text;
                    property.IsContainsID = Convert.ToBoolean(fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["是否附加ID"].Index].Value);
                    DictValues.Add(line, property);
                    sbValue.AppendLine(line);
                }
            }

            TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
            m_TranslateResult = translateResult;
            foreach (Trans_Result transResult in translateResult.trans_result)
            {
                if (DictValues.ContainsKey(transResult.src))
                {
                    string prepertyName = GetPrepertyName(transResult.dst);
                    DictValues[transResult.src].PrepertyName = prepertyName;
                }
            }
            this.txtResult.Text = GetFillValue(DictValues).ToString();
            Clipboard.SetDataObject(txtResult.Text);//把内容复制到粘贴板
        }

        /// <summary>
        /// 生成数据绑定代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWinformDataBind_Click(object sender, EventArgs e)
        {
          
            Dictionary<string, PropertyInfo> DictValues = new Dictionary<string, PropertyInfo>();

            for (int i = 0; i < fpdCode_Sheet1.RowCount; i++)
            {
                string line = fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["中文"].Index].Text;
                if (!DictValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    PropertyInfo property = new PropertyInfo();
                    property.ControlName = fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["控件类型名称"].Index].Text;
                    property.IsContainsID = Convert.ToBoolean(fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["是否附加ID"].Index].Value);
                    DictValues.Add(line, property);
                }
            }

            TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
            m_TranslateResult = translateResult;

            foreach (Trans_Result transResult in translateResult.trans_result)
            {
                if (DictValues.ContainsKey(transResult.src))
                {
                    string prepertyName = GetPrepertyName(transResult.dst);
                    DictValues[transResult.src].PrepertyName = prepertyName;
                    DictValues[transResult.src].dst = transResult.dst;
                }
            }

            StringBuilder sbValue = 获取数据绑定代码(DictValues);
            this.txtResult.Text = sbValue.ToString();
            Clipboard.SetDataObject(txtResult.Text);//把内容复制到粘贴板
        }

        /// <summary>
        /// 清零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.m_TranslateResult = null;
        }

        #region 方法
        private string GetFromValue()
        {
            StringBuilder sbValue = new StringBuilder();
            Dictionary<string, PropertyInfo> DictValues = new Dictionary<string, PropertyInfo>();

            for (int i = 0; i < fpdCode_Sheet1.RowCount; i++)
            {
                string line = fpdCode_Sheet1.Cells[i, fpdCode_Sheet1.Columns["中文"].Index].Text;
                if (!DictValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    DictValues.Add(line, new PropertyInfo());
                    sbValue.AppendLine(line);
                }
            }
            return sbValue.ToString();
        }

        private string GetPrepertyName(string dst)
        {
            char[] words = dst.ToCharArray();

            List<char> lisResultWords = new List<char>();

            //首字母大写
            if (words.Length > 0)
            {
                words[0] = char.ToUpper(words[0]);
            }

            for (int i = 0; i < words.Length; i++)
            {
                //32是空格的意思
                if (words[i] == 32)
                {
                    if (i < words.Length - 2)
                    {
                        //第二个单词 首字母大写(如Space number 应该为SpaceNumber)
                        words[i + 1] = char.ToUpper(words[i + 1]);
                    }
                    continue;
                }

                //过滤掉非字母
                if (!char.IsLetter(words[i]))
                {
                    if (i < words.Length - 2)
                    {
                        //第二个单词 首字母大写(如Space number 应该为SpaceNumber)
                        words[i + 1] = char.ToUpper(words[i + 1]);
                    }
                    continue;
                }
                lisResultWords.Add(words[i]);
            }

            string result = new string(lisResultWords.ToArray());

            return result;
        }

        private StringBuilder 获取数据绑定代码(Dictionary<string, PropertyInfo> dictValues)
        {
            StringBuilder sbValue = new StringBuilder();

            string equals = @"=""""";
            foreach (string key in dictValues.Keys)
            {
                string 控件前缀 = "";
                switch (dictValues[key].ControlName)
                {
                    case "下拉框":
                        控件前缀 = "cmb";
                        //下拉框需要添加集合 和当前值
                       string 下拉框当前值绑定代码= 获取控件数据绑定代码(key, dictValues[key].dst, "SelectedValue", 控件前缀, $"{dictValues[key].PrepertyName}CurrentValue");
                        sbValue.AppendLine(下拉框当前值绑定代码);

                        string 下拉框绑定数据源代码= 获取下拉框绑定数据源代码(控件前缀, dictValues[key].dst, $"{dictValues[key].PrepertyName}s");
                        sbValue.AppendLine(下拉框绑定数据源代码);
                        break;
                    case "文本框":
                        控件前缀 = "txt";
                        string 文本框当前值绑定代码 = 获取控件数据绑定代码(key, dictValues[key].dst, "Text", 控件前缀, $"{dictValues[key].PrepertyName}");
                        sbValue.AppendLine(文本框当前值绑定代码);
                        break;
                    case "复选框":
                        控件前缀 = "chk";
                        equals = @"=false";
                        string 复选框当前值绑定代码 = 获取控件数据绑定代码(key, dictValues[key].dst, "Checked", 控件前缀, $"{dictValues[key].PrepertyName}");
                        sbValue.AppendLine(复选框当前值绑定代码);
                        break;
                    case "数值控件":
                        控件前缀 = "num";
                        string 数值控件当前值绑定代码 = 获取控件数据绑定代码(key, dictValues[key].dst, "Text", 控件前缀, $"{dictValues[key].PrepertyName}");
                        sbValue.AppendLine(数值控件当前值绑定代码);
                        break;
                }

                if (dictValues[key].IsContainsID)
                {
                    //绑定Tag
                    string 绑定Tag代码 = 获取控件数据绑定代码(key, dictValues[key].dst, "Tag", 控件前缀, $"{dictValues[key].PrepertyName}ID");
                    sbValue.AppendLine(绑定Tag代码);
                }



            }

            return sbValue;

        }

        private StringBuilder GetFillValue(Dictionary<string, PropertyInfo> dictValues)
        {
            StringBuilder sbValue = new StringBuilder();

            string equals = @"=""""";
            foreach (string key in dictValues.Keys)
            {
             
                switch (dictValues[key].ControlName)
                {
                    case "下拉框":
                        //下拉框需要添加集合 和当前值
                        string 下拉框当前行 = GetREValue($"{key}选中值" ,"int",$"{dictValues[key].PrepertyName}CurrentValue" ,"");
                        sbValue.AppendLine(下拉框当前行);
                        string 下拉框集合 = GetREValue($"{key}集合", "List<OrderPare<int, string>>", $"{dictValues[key].PrepertyName}s", "=new List<OrderPare<int, string>>()");
                        sbValue.AppendLine(下拉框集合);
                        break;
                    case "文本框":
                        string 文本格式化之后的值 = GetREValue(key, dictValues[key].TypeName, dictValues[key].PrepertyName, @"=""""");
                        sbValue.AppendLine(文本格式化之后的值);
                        break;
                    case "复选框":
                        equals = @"=false";
                        string 复选框式化之后的值 = GetREValue(key, "bool", dictValues[key].PrepertyName, @"=false");
                        sbValue.AppendLine(复选框式化之后的值);
                        break;
                    case "数值控件":
                        string num控件格式化之后的值 = GetREValue(key, "decimal", dictValues[key].PrepertyName, @"=0");
                        sbValue.AppendLine(num控件格式化之后的值);
                        break;



                }

                if (dictValues[key].IsContainsID)
                {
                    string 复选框式化之后的值 = GetREValue($"{key}ID", "int", $"{dictValues[key].PrepertyName}ID", "");
                    sbValue.AppendLine(复选框式化之后的值);
                }

              

            }

            return sbValue;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="explan">注释</param>
        /// <param name="typename">类型</param>
        /// <param name="propertyName">属性</param>
        /// <param name="equals">等于</param>
        /// <returns></returns>
        private string GetREValue(string explan, string typename, string propertyName, string equals)
        {

            #region 自己赋值
            StringBuilder sbValue = new StringBuilder();
            sbValue.AppendLine($"/// <summary>");
            sbValue.AppendLine($"/// {explan}");
            sbValue.AppendLine($"/// </summary>");
           
                sbValue.AppendLine($"private {typename} m_{propertyName}{equals};");
           

            sbValue.AppendLine();
            sbValue.AppendLine($"/// <summary>");
            sbValue.AppendLine($"///{explan}");
            sbValue.AppendLine($"/// </summary>");
            if (chk是否添加Element.Checked)
            {
                sbValue.AppendLine($"[XmlElement(\"{explan}\")]");
            }
            sbValue.AppendLine($"public {typename} {propertyName}");
            sbValue.AppendLine("{");
            sbValue.AppendLine($"get {{ return m_{propertyName}; }}");
            sbValue.AppendLine($"set");
            sbValue.AppendLine("{");
            sbValue.AppendLine($"m_{propertyName}= value; ");
            sbValue.AppendLine($"this.RaisePropertyChanged(()=> {propertyName});");
            sbValue.AppendLine("}");
            sbValue.AppendLine("}");
            return sbValue.ToString();

            #endregion
        }

        private string 获取控件数据绑定代码(string 原文,string 译文,string 控件绑定属性,string 控件前缀,string VM属性)
        {
            StringBuilder sbValue=new StringBuilder();
            sbValue.AppendLine($"//{原文}({译文})");
            //数据绑定语句
            string prepertyName = GetPrepertyName(译文);
            sbValue.AppendLine($"this.{控件前缀}{prepertyName}.DataBindings.Add(\"{控件绑定属性}\", {this.txtObjectName.Text},");
            sbValue.AppendLine($"    PropertyHelp.GetPropertyName<{txtClassName.Text}>(x => x.{VM属性}));");
            return sbValue.ToString();
        }

        public string 获取下拉框绑定数据源代码(string 控件前缀, string 译文,string VM属性)
        {
            StringBuilder sbSql=new StringBuilder();
            string prepertyName = GetPrepertyName(译文);
            sbSql.AppendLine($"this.{控件前缀}{prepertyName}.DisplayMember = \"DisplayMember\";");
            sbSql.AppendLine($"this.{控件前缀}{prepertyName}.ValueMember = \"ValueMember\";");
            sbSql.AppendLine($"this.{控件前缀}{prepertyName}.DataSource = {this.txtObjectName.Text}.{VM属性};");
            return sbSql.ToString();
        }


        #endregion

       
    }
}
